#ifndef IMAGETRANSFERPROTOCOL_H
#define IMAGETRANSFERPROTOCOL_H

#define MAX_DATAGRAM_SIZE   1280


#define DEFAULT_SERVER_SYNC_PORT    7777
#define DEFAULT_SERVER_INFO_PORT    7787
#define DEFAULT_SERVER_DATA_PORT    7797
#define DEFAULT_CLIENT_SYNC_PORT    7877
#define DEFAULT_CLIENT_INFO_PORT    7887
#define DEFAULT_CLIENT_DATA_PORT    7897

#define REQUEST_MAGIC   0x1130
#define RESPONSE_MAGIC  0x1208

#define GET 0x0000
#define SET_GET  0x001

#define REQUEST_FIELD_MAX 0x000f

#define IMAGE_REQUEST   0x0100


#define PTZ_REQUEST     0x0200


#define CAMERA_REQUEST  0x0300


#define TRACK_REQUEST   0x0400

#define SYNC_REQUEST    0xff00

#define RESPONSE_FIELD_MAX  REQUEST_FIELD_MAX

#define IMAGE_RESPONSE  IMAGE_REQUEST

#define IMAGE_TIMESTAMP 0x0000
#define IMAGE_SEQ       0x0001
#define IMAGE_SIZE      0x0002
#define IMAGE_WIDTH     0x0003
#define IMAGE_HEIGHT    0x0004
#define IMAGE_SEQ_SIZE  0x0005
#define IMAGE_TRAN_SWITCH	0x0006
#define IMAGE_TRAN_SPEED    0x0007

#define PTZ_RESPONSE    PTZ_REQUEST

#define PTZ_X           0x0000
#define PTZ_Y           0x0001
#define PTZ_Z           0x0002

#define CAMERA_RESPONSE CAMERA_REQUEST

#define CAMERA_FOCUS    0x0000
#define CAMERA_BRIGHTNESS   0x0001
#define CAMERA_CONTRAST 0x0002
#define CAMERA_HUE      0x0003
#define CAMERA_SATURATION   0x0004
#define CAMERA_RECORD   0x0005

#define TRACK_RESPONSE  TRACK_REQUEST

#define TRACK_TIMESTAMP 0x0000
#define TRACK_PT_X1     0x0001
#define TRACK_PT_Y1     0x0002
#define TRACK_PT_X2     0x0003
#define TRACK_PT_Y2     0x0004
#define TRACK_PT_CENTER_X	0x0005
#define TRACK_PT_CENTER_Y	0x0006

#define SYNC_RESPONSE   SYNC_REQUEST

#define SYNC_TIME   0x0000
#define SYNC_COUNT  0x0001
#define SYNC_DATA_SWITCH    0x0002
#define SYNC_INFO_SWITCH    0x0003

typedef struct request_head{
    unsigned int timestamp;
    unsigned int magic;
    unsigned int type;
    unsigned int action;
}request_head_t;


#define FIELD_OFFSET    (sizeof(request_head_t) / sizeof(unsigned int))
typedef struct normal_request{
    unsigned int info[FIELD_OFFSET];
    int field[REQUEST_FIELD_MAX];
}normal_request_t;

#define requestInfo(x)  ((request_head_t*)(x))
#define getRequestField(x, i)  (((normal_request_t*)(x))->field[(i)])
#define setRequestField(x, i, v)	(((normal_request_t*)(x))->field[(i)] = (v))

#define response_head_t request_head_t
typedef struct response_head{
    unsigned int info[FIELD_OFFSET];
    int field[RESPONSE_FIELD_MAX];
}normal_response_t;

#define responseInfo(x)         ((response_head_t*)(x))
#define getResponseField(x, i)  (((normal_response_t*)(x))->field[(i)])
#define setResponseField(x, i, v)	(((normal_response_t*)(x))->field[(i)] = (v))

#define RESPONSE_DATA_LEN   (MAX_DATAGRAM_SIZE - RESPONSE_FIELD_MAX * sizeof(int) - FIELD_OFFSET * sizeof(unsigned int))
typedef struct response_data{
    unsigned int info[FIELD_OFFSET];
    int field[RESPONSE_FIELD_MAX];//magic code check
    unsigned char data[RESPONSE_DATA_LEN];
}response_data_t;

#define getResponseDataInfo(x, i)   (((response_data_t*)(x))->field[(i)])
#define setResponseDataInfo(x, i, v)	(((response_data_t*)(x))->field[(i)] = (v))
#define responseDataPtr(x)	(&(((response_data_t*)(x))->data[0]))

#endif // IMAGETRANSFERPROTOCOL_H

